/**   
* @Title: ChildStateJobTimerIStempImpl.java 
* @Package cn.com.commons.quartzframe.job 
* @Description: 执行子任务的执行器实现
* @author chenmin-ds   
* @version V1.0   
*/ 


package com.gomeplus.commons.quartz.job;

import com.gomeplus.commons.quartz.cache.QuartzTaskConfigCache;
import com.gomeplus.commons.quartz.task.ITask;
import com.gomeplus.frame.factory.SpringBeanFactory;
import net.sf.json.JSONObject;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.StatefulJob;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/** 
 * @ClassName: ExcuteNowTemplate
 * @Description: 立即执行的模板
 * @author zhumenglong
 */
@SuppressWarnings("deprecation")
public class ExcuteNowTemplate {

	private Logger logger = LoggerFactory.getLogger("ExcuteNowTemplate");
	
	/* (非 Javadoc) 
	 * <p>Title: execute</p> 
	 * <p>Description: </p> 
	 * @param beanName
	 * @throws JobExecutionException 
	 * @see org.quartz.Job#execute(org.quartz.JobExecutionContext) 
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void execute(String beanName) throws JobExecutionException {
		try {
			Object objIStep = SpringBeanFactory.getBean(beanName);
			if(null == objIStep){
				logger.info("没有找到立即可执行的任务对象，任务名称【{}】" , beanName);
			}else{
				ITask istep = (ITask) objIStep;
				List<Object> list = istep.queryTaskInfo(new JSONObject());
				if (list != null) {
					for (Object temp : list) {
						istep.runTask(temp);
					}
					istep.editTaskInfo(list);
					list.clear();
				} else {
					logger.info("没有查询到需要执行的记录，任务对象名称【{}】" , beanName);
				}
			}
		} catch (Exception e) {
			logger.error("任务执行异常：" + beanName, e);
		}
	}
}
